package com.heima.kafka.sample;

import org.apache.kafka.clients.producer.KafkaProducer;
import org.apache.kafka.clients.producer.ProducerConfig;
import org.apache.kafka.clients.producer.ProducerRecord;

import java.util.Properties;

/**
 * @Author: xsz
 * @DateTime: 2024/3/1 0:23
 **/
public class ProducerQuickStart {

    public static void main(String[] args) {
        //kafka连接配置
        Properties prop = new Properties();
        prop.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG,"192.168.200.130:9092");
        prop.put(ProducerConfig.RETRIES_CONFIG,5);

        prop.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG,"org.apache.kafka.common.serialization.StringSerializer");
        prop.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG,"org.apache.kafka.common.serialization.StringSerializer");

        //创建kafka生产对象
        KafkaProducer<String,String> producer = new KafkaProducer<String, String>(prop);

        //发送消息
        /**
         * 第一个参数 topic
         * 第二个参数 消息的key
         * 第三个参数 消息的value
         */
        ProducerRecord<String, String> kvProducerRecord = new ProducerRecord<String,String>("topic-first","key-001","hello kafka");
        System.out.println("发送");
        producer.send(kvProducerRecord);

        //关闭消息通道 必须关闭，否则消息发送不成功
        System.out.println("关闭消息通道 必须关闭，否则消息发送不成功");
        producer.close();
    }

}
